<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <commentltem @click_reply="$emit('click_reply',$event)" :comment="item" v-for="item in list" :key="item.com_id + ''" :title="item.content"/>
  </van-list>
</template>

<script>
import { getComment } from '@/api/user.js'
import commentltem from '@/components/commentltem'
export default {
  components: {
    commentltem
  },
  data () {
    return {

      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      // 对象 字符串 数字
      type: [Object, String, Number],
      require: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  created () {

  },

  methods: {
    async onLoad () {
      try {
        const { results, last_id: lastdid, total_count: total } = await getComment({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: this.limit
        })
        // 复杂类型是同步的，所有可以push进父组件中
        this.list.push(...results)
        // 加载结束
        this.loading = false
        // 开启下一个
        this.offset = lastdid
        this.$emit('total-cot', total)
        if (!results.length) {
          this.finished = true
        }
      } catch (err) {
        console.dir(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
